package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FragmentUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.adapter.TypeAdapter;
import flc.ast.bean.TypeBean;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private TypeAdapter typeAdapter;
    private int oldPosition;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private HotFragment hotFragment;
    private MovieFragment movieFragment;
    private TeleplayFragment teleplayFragment;
    private VarietyFragment varietyFragment;
    private AnimationFragment animationFragment;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.llSearch.setOnClickListener(this);

        mDataBinding.rvTypeList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        typeAdapter = new TypeAdapter();
        mDataBinding.rvTypeList.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(this);

        hotFragment = new HotFragment();
        movieFragment = new MovieFragment();
        teleplayFragment = new TeleplayFragment();
//        varietyFragment = new VarietyFragment();
        animationFragment = new AnimationFragment();
        mFragmentList.add(hotFragment);
        mFragmentList.add(movieFragment);
        mFragmentList.add(teleplayFragment);
//        mFragmentList.add(varietyFragment);
        mFragmentList.add(animationFragment);
        FragmentUtils.add(getChildFragmentManager(), mFragmentList, R.id.fl_container, 0);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSearch://搜索
                startActivity(SearchActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        typeAdapter.getItem(oldPosition).setSelected(false);
        typeAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        typeAdapter.getItem(position).setSelected(true);
        typeAdapter.notifyItemChanged(position);
        FragmentUtils.showHide(position, mFragmentList);
    }

    @Override
    protected void initData() {
        List<TypeBean> list = new ArrayList<>();
        list.add(new TypeBean(getString(R.string.home_type1), true));
        list.add(new TypeBean(getString(R.string.home_type2), false));
        list.add(new TypeBean(getString(R.string.home_type3), false));
//        list.add(new TypeBean(getString(R.string.home_type4), false));
        list.add(new TypeBean(getString(R.string.home_type5), false));
        typeAdapter.setList(list);
        getBannerData();
    }

    private void getBannerData() {
        StkResApi.getTagResourceList(this, AppConfig.BANNER_DATA, StkResApi.createParamMap(0, 6), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        MyBannerAdapter myMovieBannerAdapter = new MyBannerAdapter(result, mContext);
                        mDataBinding.banner.setAdapter(myMovieBannerAdapter, true);//false不无限循环
                        mDataBinding.banner.setBannerGalleryEffect(20, 20, 10, 1);
                        mDataBinding.banner.isAutoLoop(true);//false不自动轮播
                        mDataBinding.banner.setIndicator(new RectangleIndicator(mContext));
                        mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object data, int position) {
                                StkResBean bean = (StkResBean) mDataBinding.banner.getAdapter().getData(position);
                                MovieDetailActivity.stkResBean = bean;
                                startActivity(MovieDetailActivity.class);
                            }
                        });
                    }
                }
            }
        });
    }

}